#include "sectionmodel.h"

SectionModel::SectionModel(QObject *parent) : QAbstractTableModel(parent)
{
}

void SectionModel::addSections(QList<FileSection *>secs)
{
    sections.append(secs);
}

int SectionModel::rowCount(const QModelIndex &parent)  const
{
    return sections.count();
}

int SectionModel::columnCount(const QModelIndex &parent) const
{
    return 3;
}

QVariant SectionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole || orientation == Qt::Vertical)
        return QVariant();
    switch (section) {
         case 0:
             return tr("Name");
         case 1:
             return tr("Start Address");
         case 2:
             return tr("End Address");
         default:
             return QVariant();
     }
}

QVariant SectionModel::data(const QModelIndex &index, int role)  const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    FileSection *section = sections.at(index.row());

    if(index.column() == 0)
    {
        return section->getSectionName();
    }
    else if(index.column() == 1)
    {
        return QString("%1").arg((uint)section->getHandle(), 8, 16, QChar('0'));
    }
    else if(index.column() == 2)
    {
        return QString("%1").arg((uint)section->getHandle() + section->getSize() - 1, 8, 16, QChar('0'));
    }
    return QVariant();
}
